package com.crb.smartcard.utils;

import android.app.Application;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

public class AppUtil {

    private static Application mApplication;

    private static final String TAG = "APPUtil";

    private static final String SP_KEY_SEID_DATA = "sp_key_seid_data";

    /**
     * 初始化
     * @param context 上下文
     */
    public static void init(Application context){
        mApplication = context;
    }

    public static Application getApplication(){
        return mApplication;
    }

    /**
     * 获取所有缓存的已开卡的 seid 数据
     * @return 已开卡的 seid 缓存集合
     */
    public static List<String> getAllSeIdCacheData(){
        return SharePreferencesUtil.getStrListValue(SP_KEY_SEID_DATA);
    }

    public static void addSeIdCache(String seId){
        if(TextUtils.isEmpty(seId)){
//            throw new IllegalArgumentException("seId must not null");
            LogUtil.e(TAG,"===>>seId must not null");
            return;
        }
        List<String> list = getAllSeIdCacheData();
        if(list.size() > 0){
            List<String> oldList = SharePreferencesUtil.getStrListValue(SP_KEY_SEID_DATA);
            LogUtil.e(TAG,"====>>addSeIdCache()-->>oldList:"+oldList.toString());
            if(oldList.contains(seId)){
                LogUtil.e("====>>addSeIdCache()-->>seId 缓存已存在 seid:"+seId);
            }else{
                oldList.add(seId);
                SharePreferencesUtil.putStrListValue(SP_KEY_SEID_DATA,oldList);
                LogUtil.e("====>>addSeIdCache()-->>oldList 已添加 seid:"+seId);
            }
        }else{
            List<String> newList = new ArrayList<>();
            newList.add(seId);
            SharePreferencesUtil.putStrListValue(SP_KEY_SEID_DATA,newList);
            LogUtil.e("====>>addSeIdCache()-->newList 添加 seId:"+seId);
        }
    }

    /**
     * 移除seid缓存
     * @param seId
     */
    public static void removeSeIdCache(String seId){
        if(TextUtils.isEmpty(seId)){
//            throw new IllegalArgumentException("seId must not null");
            LogUtil.e(TAG,"===>>seId must not null");
            return;
        }
        List<String> list = getAllSeIdCacheData();
        if(list.size() > 0){
            List<String> oldList = SharePreferencesUtil.getStrListValue(SP_KEY_SEID_DATA);
            LogUtil.e(TAG,"====>>removeSeIdCache()-->>oldList:"+oldList.toString());
            if(oldList.contains(seId)){
                oldList.remove(seId);
                SharePreferencesUtil.putStrListValue(SP_KEY_SEID_DATA,oldList);
                LogUtil.e("====>>removeSeIdCache()-->>oldList 中 移除 seId 成功，seid："+seId);
                LogUtil.e(TAG,"====>>removeSeIdCache()-->>oldList:"+oldList.toString());
            }else{
                LogUtil.e("====>>removeSeIdCache()-->>oldList 不包含 seid："+seId);
            }
        }else{
            LogUtil.e("====>>removeSeIdCache()-->SeIdCacheDataList is empty");
        }
    }

    /**
     * 清除所有seid缓存
     */
    public static void clearAllSeIdCache(){
        SharePreferencesUtil.removeStrList(SP_KEY_SEID_DATA);
    }


    /**
     * seid 缓存对象
     * 以前想说，封装一个 seId 的对象，利于扩展。
     * 后来发现，直接存seid即可，只要有数据，就认为是已开卡的缓存即可。
     * 所以这个对象，先写在这里，用不到用不到的以后再说。
     */
    public static class SEID{
        public static final int CARD_STATUS_UNACTIVATED = 0;
        public static final int CARD_STATUS_ACTIVATED = 1;
        private String seid;
        private int cardStatus = -1;

        public String getSeid() {
            return seid;
        }

        public void setSeid(String seid) {
            this.seid = seid;
        }

        public int getCardStatus() {
            return cardStatus;
        }

        public void setCardStatus(int cardStatus) {
            this.cardStatus = cardStatus;
        }
    }

}
